﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BackGround_DetailInfo : System.Web.UI.Page
{
    Operation operation = new Operation();
    int id;
    FCKLoadedManager mg;
    protected void Page_Load(object sender, EventArgs e)
    {
        id = Convert.ToInt32(Request.QueryString["id"].ToString());
        if (Session["userid"] == null)
            Response.Write("<script language=javascript>alert('会话失效，请重新登录！');</script>");
        else if (!IsPostBack)
        {
            DataSet ds = operation.SelectPublicInfoById(id,false);
            String Type = ds.Tables[0].Rows[0][1].ToString();
            one.Text = Type;
            txtTitle.Text = ds.Tables[0].Rows[0][2].ToString();
            txtInfo.Value = ds.Tables[0].Rows[0][3].ToString();
            txtLinkMan.Text = ds.Tables[0].Rows[0][4].ToString();
            txtTel.Text = ds.Tables[0].Rows[0][5].ToString();
            BuildList(new object(),new EventArgs());
            notXQInfoPanel.Visible = true;
            XQInfoPanel.Visible = false;
            if (one.Text.IndexOf("技术") != -1)
            {
                try
                {
                    dlCategory.SelectedValue = ds.Tables[0].Rows[0][9].ToString();
                }
                catch (Exception ex) { }
                if (one.Text.IndexOf("需求") != -1)
                {
                    notXQInfoPanel.Visible = false;
                    XQInfoPanel.Visible = true;
                    tbPrjMan.Text = txtLinkMan.Text;
                    tbPrjName.Text = txtTitle.Text;
                    tbWayReq.Text = txtInfo.Value;
                    tbTel.Text = txtTel.Text;
                    tbComName.Text = ds.Tables[0].Rows[0][10].ToString();
                    tbDept.Text = ds.Tables[0].Rows[0][11].ToString();
                    tbRanks.Text = ds.Tables[0].Rows[0][12].ToString();
                    tbAddress.Text = ds.Tables[0].Rows[0][13].ToString();
                    tbPostCode.Text = ds.Tables[0].Rows[0][14].ToString();
                    tbPhone.Text = ds.Tables[0].Rows[0][15].ToString();
                    tbFx.Text = ds.Tables[0].Rows[0][16].ToString();
                    tbEmail.Text = ds.Tables[0].Rows[0][17].ToString();
                    tbProblem.Text = ds.Tables[0].Rows[0][18].ToString();
                    tbComInfo.Text = ds.Tables[0].Rows[0][19].ToString();
                    tbNewPrd.Text = ds.Tables[0].Rows[0][20].ToString();
                    tbWill.Text = ds.Tables[0].Rows[0][21].ToString();
                }
                else
                {
                    mg = new FCKLoadedManager(Session["userid"].ToString());
                    mg.deleteImages();
                    if (Session["imgs"] != null) Session.Remove("imgs");
                    Session.Add("oldimgs", mg.getFileListFromInfo(txtInfo.Value.Trim()));
                }
            }
        }
    }
    
    public void BuildList(object sender, EventArgs e)
    {
        if(one.Text.IndexOf("技术")==-1)
        {
            dlCategory.Visible=false;
        }
        else
        {
            dlCategory.Visible=true;
            dlCategory.Items.Clear();
            BuildDownList("0", "", "---");
        }
    }
    public void BuildDownList(string pid, string itH, string dx)//itH为表项前度，dx为递增前度
    {
        DataSet ds = operation.SelectCategoryByPID(pid,true);//一级类别
        int count = ds.Tables[0].Rows.Count;
        string t = itH + dx;//下级前度
        for (int i = 0; i < count; i++)
        {
            ListItem item = new ListItem();
            item.Value = ds.Tables[0].Rows[i][0].ToString();
            item.Text = itH + ds.Tables[0].Rows[i][1].ToString();
            dlCategory.Items.Add(item);
            BuildDownList(item.Value, t, dx);
        }
    }
    protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        string typeid = dlCategory.SelectedValue;
        if (typeid.Length <= 0) typeid = "0";
        if (one.Text.IndexOf("需求") != -1)
        {
            operation.UpdateXQInfo(id.ToString(), tbComName.Text.Trim(), tbPrjName.Text.Trim(), tbPrjMan.Text.Trim(),
                tbDept.Text.Trim(), tbRanks.Text.Trim(), tbAddress.Text.Trim(), tbPostCode.Text.Trim(), tbTel.Text.Trim(), tbPhone.Text.Trim(),
                tbFx.Text.Trim(), tbEmail.Text.Trim(), tbWayReq.Text.Trim(), tbComInfo.Text.Trim(), tbProblem.Text.Trim(), tbNewPrd.Text.Trim(),
                tbWill.Text.Trim());
        }
        else
        {
            mg = new FCKLoadedManager(Session["userid"].ToString());
            string oldlist = Session["oldimgs"].ToString();
            if (oldlist != "")
            {
                string list = mg.getFileListFromInfo(txtInfo.Value.Trim())+"|";
                if (list != "")
                {
                    string[] oldlists = oldlist.Split('|');
                    string name;
                    try
                    {
                        for (int i = oldlists.Length - 1; i >= 0; i--)
                        {
                            if (list.IndexOf(oldlists[i]) == -1)
                            {
                                name = oldlists[i].Substring(oldlists[i].LastIndexOf("/") + 1);
                                if (System.IO.File.Exists(mg.uploadurl + name))
                                {
                                    System.IO.File.Delete(mg.uploadurl + name);
                                }
                            }
                        }
                        Session.Remove("oldimgs");
                    }
                    catch (Exception exp) { }
                }
            }
            mg.deleteImgBeforeInsert(txtInfo.Value.Trim());
            operation.UpdatePublicInfo(one.Text, txtTitle.Text.Trim(), txtInfo.Value.Trim(), txtLinkMan.Text.Trim(), txtTel.Text.Trim(), typeid, id.ToString());
        }
       WebMessageBox.Show("信息修改成功！", "UnCheckInfo.aspx");
    }
}
